package com.jn.exam.question.service.impl;

import com.jn.exam.HTMLUtil;
import com.jn.exam.question.entity.PersonalQuestion;
import com.jn.exam.question.entity.PersonalQuestionStore;
import com.jn.exam.question.entity.Question;
import com.jn.exam.question.entity.vo.QuestionWrapper;
import com.jn.exam.question.mapper.PersonalQuestionMapper;
import com.jn.exam.question.mapper.PersonalQuestionStoreMapper;
import com.jn.exam.question.mapper.QuestionMapper;
import com.jn.exam.question.service.OssFeignService;
import com.jn.exam.question.service.PersonalQuestionStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-01-18
 */
@Service
public class PersonalQuestionStoreServiceImpl extends ServiceImpl<PersonalQuestionStoreMapper, PersonalQuestionStore> implements PersonalQuestionStoreService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private PersonalQuestionMapper personalQuestionMapper;

    @Autowired
    private OssFeignService ossFeignService;


    @Override
    public boolean deleteRelationship(String personalQuestionStoreId, String teacherId) {
        return baseMapper.deleteRelationship(personalQuestionStoreId, teacherId);
    }

    @Override
    public List<String> selectPersonalQuestionIds(String personalQuestionStoreId, String teacherId) {
        return baseMapper.selectPersonalQuestionIds(personalQuestionStoreId, teacherId);
    }

    @Override
    public List<Map<String, Integer>> getPersonalQuestionStoreTypeNumber(String personalQuestionStoreId) {
        return baseMapper.getPersonalQuestionStoreTypeNumber(personalQuestionStoreId);
    }

    @Override
    public List<PersonalQuestion> pageSelect(QuestionWrapper questionWrapper) {
        return baseMapper.pageSelect(questionWrapper);
    }

    @Override
    public Integer pageTotal(QuestionWrapper questionWrapper) {
        return baseMapper.pageTotal(questionWrapper);
    }

    @Override
    public boolean removeByPersonalQuestionStoreId(String personalQuestionStoreId) {
        return baseMapper.removeByPersonalQuestionStoreId(personalQuestionStoreId);
    }

    @Async
    @Override
    public Future<Boolean> addQuestionToPersonalStore(Question question, String personalQuestionStoreId) {
        String name = question.getName();
        List<String> nameUrl = HTMLUtil.getUrl(name);
        if (!nameUrl.isEmpty()){
            for (String u:nameUrl){
                String newUrl = ossFeignService.copyFileByUrl(u);
                name=name.replaceAll(u,newUrl);
            }
        }
        String answer = question.getAnswer();
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        if (!answerUrl.isEmpty()){
            for (String u:answerUrl){
                String newUrl = ossFeignService.copyFileByUrl(u);
                answer=answer.replaceAll(u,newUrl);
            }
        }
        PersonalQuestion personalQuestion=new PersonalQuestion();
        personalQuestion.setName(name);
        personalQuestion.setAnswer(answer);
        personalQuestion.setType(question.getType());
        personalQuestion.setMajor(question.getType());
        personalQuestion.setSubject(question.getSubject());
        personalQuestion.setLevel(question.getLevel());
        personalQuestion.setStoreId(personalQuestionStoreId);
        int insert = personalQuestionMapper.insert(personalQuestion);
        if (insert==1){
            return new AsyncResult<>(true);
        }

        return new AsyncResult<>(false);
    }
}
